package com.huaxinkaiye.activity;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.http.NameValuePair;
import org.apache.http.message.BasicNameValuePair;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import com.android.volley.toolbox.NetworkImageView;
import com.google.gson.Gson;
import com.huaxinkaiye.R;
import com.huaxinkaiye.application.HuaXinApplication;
import com.huaxinkaiye.bean.KongPositionBean;
import com.huaxinkaiye.bean.RowsBean;
import com.huaxinkaiye.lookbigphoto.PhotosDialog;
import com.huaxinkaiye.utils.ProgressUtil;
import com.huaxinkaiye.utils.SimpleClient;
import com.huaxinkaiye.utils.ToastUtil;
import com.huaxinkaiye.utils.UrlUtils;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.ImageView;
import android.widget.SimpleAdapter;
import android.widget.Spinner;
import android.widget.TextView;
import android.widget.Toast;

public class CarRuKuDengJiActivity2 extends Activity implements OnClickListener {
	private ImageView im_back, im_cancle, im_save;
	private Spinner daqu_spinner0, daqu_spinner, paishu_spinner, geshu_spinner,
			cengshu_spinner;
	private String picUrl = "";
	public NetworkImageView im_che;
	private TextView te_chepaihao, te_chuangjiantime, te_danganbianhao,
			te_daqu, te_paishu, te_geshu, te_chengshu;
	private RowsBean rowsBean;
	public ArrayList<String> arr1;
	public ArrayList<String> arr2;
	public ArrayList<String> arr3;
	public ArrayList<String> arr4;
	public List<NameValuePair> params;
	public ProgressUtil progress;
	Handler handler = new Handler() {
		public void handleMessage(Message msg) {
			switch (msg.what) {
			case 1:
				jiexiListData(msg.obj.toString());
				break;
			case 2:
				Jiexikongweizhi(msg.obj.toString());
				break;
			}
		}
	};

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		progress = new ProgressUtil(this, "正在获取数据");
		setContentView(R.layout.activity_rukudengji);
		initView();
	}

	protected void Jiexikongweizhi(String s) {
		arr1 = new ArrayList<String>();
		try {
			JSONArray json = new JSONArray(s);
			Gson gson = new Gson();
			for (int i = 0; i < json.length(); i++) {
				KongPositionBean bean = gson.fromJson(json.get(i).toString(),
						KongPositionBean.class);
				arr1.add(bean.getRegion() + bean.getRow() + bean.getNumber()
						+ bean.getLayer());
			}
			List<Map<String, Object>> data = new ArrayList<Map<String, Object>>();
			for (int i = 0; i < arr1.size(); i++) {
				Map<String, Object> map = new HashMap<String, Object>();
				map.put("text", arr1.get(i));
				data.add(map);
			}
			SimpleAdapter simpleAdapter = new SimpleAdapter(this, data,
					R.layout.spinner, new String[] { "text" },
					new int[] { R.id.textView1 });
			daqu_spinner0.setAdapter(simpleAdapter);
			progress.dismiss();
			// 注册事件
			daqu_spinner0
					.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {

						@Override
						public void onNothingSelected(AdapterView<?> parent) {
							Toast.makeText(getApplicationContext(), "没有改变的处理",
									Toast.LENGTH_LONG).show();
						}

						@Override
						public void onItemSelected(AdapterView<?> arg0,
								View arg1, int arg2, long arg3) {
							// TODO Auto-generated method stub
							Spinner spinner = (Spinner) arg0;
							CharSequence String;
							String postionid = spinner
									.getItemAtPosition(arg2)
									.toString()
									.substring(
											6,
											spinner.getItemAtPosition(arg2)
													.toString().length() - 1);
							te_daqu.setText(postionid.substring(0, 1));
							te_paishu.setText(postionid.substring(1, 2));
							te_geshu.setText(postionid.substring(2, 3));
							te_chengshu.setText(postionid.substring(3, 4));
							// Toast.makeText(getApplicationContext(),
							// ""+spinner.getItemAtPosition(arg2),
							// Toast.LENGTH_LONG).show();
						}
					});
		} catch (JSONException e1) {
			// TODO Auto-generated catch block
			e1.printStackTrace();
		}
	}

	protected void Jiexikongweizhi1() {
		int i1 = 0, i2 = 0, i3 = 0, i4 = 0;
		// TODO Auto-generated method stub
		// if (rowsBean.get == 96) {
		// sp_spinner.setSelection(0);
		// i = 0;
		// }
		// if (rowsBean.getBarginType().getId() == 97) {
		// sp_spinner.setSelection(1);
		// i = 1;
		// }
		// if (rowsBean.getBarginType().getId() == 98) {
		// sp_spinner.setSelection(2);
		// i = 2;
		// }
		final String arr1[] = new String[] { "A", "B", "C", "D", "E", "F", "G",
				"H", "I", "J", "K", "L", "M", "N", "O", "P", "Q", "R", "S",
				"T", "U", "V", "W", "X", "Y", "Z" };

		final String arr2[] = new String[] { "1", "2", "3", "4", "5", "6", "7",
				"8", "9" };

		final String arr3[] = new String[] { "1", "2", "3", "4", "5", "6", "7",
				"8", "9", "A", "B", "C", "D", "E", "F", "G", "H", "I", "J",
				"K", "L", "M", "N", "O", "P", "Q", "R", "S", "T", "U", "V",
				"W", "X", "Y", "Z" };
		final String arr4[] = new String[] { "A", "B", "C" };
		if (rowsBean.getStoreCode() != null) {
			for (int i = 0; i < arr1.length; i++) {
				if (rowsBean.getStoreCode().substring(0, 1).equals(arr1[i])) {
					i1 = i;
				}
			}
			for (int i = 0; i < arr2.length; i++) {
				if (rowsBean.getStoreCode().substring(1, 2).equals(arr2[i])) {
					i2 = i;
				}
			}
			for (int i = 0; i < arr3.length; i++) {
				if (rowsBean.getStoreCode().substring(2, 3).equals(arr3[i])) {
					i3 = i;
				}
			}
			for (int i = 0; i < arr4.length; i++) {
				if (rowsBean.getStoreCode().substring(3, 4).equals(arr4[i])) {
					i4 = i;
				}
			}
		}
		// ArrayAdapter<String> arrayAdapter = new ArrayAdapter<String>(this,
		// android.R.layout.simple_spinner_item, arr1);
		List<Map<String, Object>> data = new ArrayList<Map<String, Object>>();
		for (int i = 0; i < arr1.length; i++) {
			Map<String, Object> map = new HashMap<String, Object>();
			map.put("text", arr1[i]);
			data.add(map);
		}
		SimpleAdapter simpleAdapter = new SimpleAdapter(this, data,
				R.layout.spinner, new String[] { "text" },
				new int[] { R.id.textView1 });
		daqu_spinner.setAdapter(simpleAdapter);

		List<Map<String, Object>> data2 = new ArrayList<Map<String, Object>>();

		for (int i = 0; i < arr2.length; i++) {
			Map<String, Object> map2 = new HashMap<String, Object>();
			map2.put("text", arr2[i]);
			data2.add(map2);
		}
		SimpleAdapter simpleAdapter2 = new SimpleAdapter(this, data2,
				R.layout.spinner, new String[] { "text" },
				new int[] { R.id.textView1 });
		paishu_spinner.setAdapter(simpleAdapter2);

		List<Map<String, Object>> data3 = new ArrayList<Map<String, Object>>();

		for (int i = 0; i < arr3.length; i++) {
			Map<String, Object> map3 = new HashMap<String, Object>();
			map3.put("text", arr3[i]);
			data3.add(map3);
		}
		SimpleAdapter simpleAdapter3 = new SimpleAdapter(this, data3,
				R.layout.spinner, new String[] { "text" },
				new int[] { R.id.textView1 });
		geshu_spinner.setAdapter(simpleAdapter3);

		List<Map<String, Object>> data4 = new ArrayList<Map<String, Object>>();

		for (int i = 0; i < arr4.length; i++) {
			Map<String, Object> map4 = new HashMap<String, Object>();
			map4.put("text", arr4[i]);
			data4.add(map4);
		}
		SimpleAdapter simpleAdapter4 = new SimpleAdapter(this, data4,
				R.layout.spinner, new String[] { "text" },
				new int[] { R.id.textView1 });
		cengshu_spinner.setAdapter(simpleAdapter4);

		// 注册事件
		daqu_spinner
				.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {

					@Override
					public void onNothingSelected(AdapterView<?> parent) {
						Toast.makeText(getApplicationContext(), "没有改变的处理",
								Toast.LENGTH_LONG).show();
					}

					@Override
					public void onItemSelected(AdapterView<?> arg0, View arg1,
							int arg2, long arg3) {
						// TODO Auto-generated method stub
						Spinner spinner = (Spinner) arg0;
						te_daqu.setText(spinner
								.getItemAtPosition(arg2)
								.toString()
								.substring(
										6,
										spinner.getItemAtPosition(arg2)
												.toString().length() - 1));
						// Toast.makeText(getApplicationContext(),
						// ""+spinner.getItemAtPosition(arg2),
						// Toast.LENGTH_LONG).show();
					}
				});

		paishu_spinner
				.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {

					@Override
					public void onNothingSelected(AdapterView<?> parent) {
						Toast.makeText(getApplicationContext(), "没有改变的处理",
								Toast.LENGTH_LONG).show();
					}

					@Override
					public void onItemSelected(AdapterView<?> arg0, View arg1,
							int arg2, long arg3) {
						// TODO Auto-generated method stub
						Spinner spinner = (Spinner) arg0;
						te_paishu.setText(spinner
								.getItemAtPosition(arg2)
								.toString()
								.substring(
										6,
										spinner.getItemAtPosition(arg2)
												.toString().length() - 1));
						// Toast.makeText(getApplicationContext(),
						// ""+spinner.getItemAtPosition(arg2),
						// Toast.LENGTH_LONG).show();
					}
				});

		geshu_spinner
				.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {

					@Override
					public void onNothingSelected(AdapterView<?> parent) {
						Toast.makeText(getApplicationContext(), "没有改变的处理",
								Toast.LENGTH_LONG).show();
					}

					@Override
					public void onItemSelected(AdapterView<?> arg0, View arg1,
							int arg2, long arg3) {
						// TODO Auto-generated method stub
						Spinner spinner = (Spinner) arg0;
						te_geshu.setText(spinner
								.getItemAtPosition(arg2)
								.toString()
								.substring(
										6,
										spinner.getItemAtPosition(arg2)
												.toString().length() - 1));
						// Toast.makeText(getApplicationContext(),
						// ""+spinner.getItemAtPosition(arg2),
						// Toast.LENGTH_LONG).show();
					}
				});

		cengshu_spinner
				.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {

					@Override
					public void onNothingSelected(AdapterView<?> parent) {
						Toast.makeText(getApplicationContext(), "没有改变的处理",
								Toast.LENGTH_LONG).show();
					}

					@Override
					public void onItemSelected(AdapterView<?> arg0, View arg1,
							int arg2, long arg3) {
						// TODO Auto-generated method stub
						Spinner spinner = (Spinner) arg0;
						te_chengshu.setText(spinner
								.getItemAtPosition(arg2)
								.toString()
								.substring(
										6,
										spinner.getItemAtPosition(arg2)
												.toString().length() - 1));
						// Toast.makeText(getApplicationContext(),
						// ""+spinner.getItemAtPosition(arg2),
						// Toast.LENGTH_LONG).show();
					}
				});
		daqu_spinner.setSelection(i1);
		paishu_spinner.setSelection(i2);
		geshu_spinner.setSelection(i3);
		cengshu_spinner.setSelection(i4);
	}

	public void jiexiListData(String s) {
		// TODO Auto-generated method stub
		progress.dismiss();
		Log.e("ruku_save_data", s);
		JSONObject json;
		try {
			json = new JSONObject(s);
			if (json.getBoolean("isSuccess")) {
				ToastUtil.show(this, json.getString("message"));
				Intent intent = new Intent(this, CarRuKuDengJiActivity.class);
				setResult(RESULT_OK, intent);
				finish();
			} else {
				ToastUtil.show(this, json.getString("message"));
			}
		} catch (JSONException e1) {
			// TODO Auto-generated catch block
			e1.printStackTrace();
		}

	}

	private void initView() {
		// TODO Auto-generated method stub
		im_back = (ImageView) findViewById(R.id.im_back);
		im_back.setOnClickListener(this);
		im_cancle = (ImageView) findViewById(R.id.im_cancle);
		im_cancle.setOnClickListener(this);
		im_save = (ImageView) findViewById(R.id.im_save);
		im_save.setOnClickListener(this);
		im_che = (NetworkImageView) findViewById(R.id.im_che);
		im_che.setOnClickListener(this);
		te_chepaihao = (TextView) findViewById(R.id.te_chepaihao);
		te_chuangjiantime = (TextView) findViewById(R.id.te_chuangjiantime);
		te_danganbianhao = (TextView) findViewById(R.id.te_danganbianhao);

//		te_daqu = (TextView) findViewById(R.id.te_daqu);
//		te_paishu = (TextView) findViewById(R.id.te_paishu);
//		te_geshu = (TextView) findViewById(R.id.te_geshu);
//		te_chengshu = (TextView) findViewById(R.id.te_chengshu);
		daqu_spinner0 = (Spinner) findViewById(R.id.daqu_spinner0);
		daqu_spinner = (Spinner) findViewById(R.id.daqu_spinner);
		paishu_spinner = (Spinner) findViewById(R.id.paishu_spinner);
		geshu_spinner = (Spinner) findViewById(R.id.geshu_spinner);
		cengshu_spinner = (Spinner) findViewById(R.id.cengshu_spinner);
		initData();
	}

	private void initData() {
		// TODO Auto-generated method stub
		rowsBean = (RowsBean) getIntent().getSerializableExtra("rowsBeanInfo");
		for (int i = 0; i < rowsBean.getPictures().size(); i++) {
			if (rowsBean.getPictures().get(i).getFileType() == 1) {
				picUrl = UrlUtils.host
						+ rowsBean.getPictures().get(i).getFilepath();
				im_che.setImageUrl(picUrl, HuaXinApplication.mImageLoader);
			}
		}
		te_chepaihao.setText("车牌号： " + rowsBean.getPlateNumber());
		te_chuangjiantime.setTag("创建时间： " + rowsBean.getCreateTime());
		te_danganbianhao.setText("档案编号： " + rowsBean.getBarCode());

		// initspinner();
		Jiexikongweizhi1();
		// if(rowsBean.getStoreCode()!=null||(!rowsBean.getStoreCode().endsWith(""))){
		// te_daqu.setText(rowsBean.getStoreCode().substring(0, 1));
		// te_paishu.setText(rowsBean.getStoreCode().substring(1, 2));
		// te_geshu.setText(rowsBean.getStoreCode().substring(2, 3));
		// te_chengshu.setText(rowsBean.getStoreCode().substring(3, 4));
		// }
	}

	private void initspinner() {
		// TODO Auto-generated method stub
		if (HuaXinApplication.isNetWorkConnected()) {
			String url = UrlUtils.kongweizhi_url;
			progress.show();
			params = new ArrayList<NameValuePair>();
			params.add(new BasicNameValuePair("", ""));
			new Thread() {

				@Override
				public void run() {
					// TODO Auto-generated method stub
					super.run();
					String data;
					try {
						data = SimpleClient.doPost(UrlUtils.kongweizhi_url,
								params);
						Log.e("kongweizhi_url_data", data);
						Message mes = new Message();
						mes.what = 2;
						mes.obj = data;
						handler.sendMessage(mes);
					} catch (Exception e) {
						// TODO Auto-generated catch block
						e.printStackTrace();
					}
				}

			}.start();
		} else {
			Toast.makeText(this, "网络异常", Toast.LENGTH_SHORT).show();
		}
	}

	@Override
	protected void onResume() {
		// TODO Auto-generated method stub
		super.onResume();
	}

	@Override
	public void onClick(View v) {
		// TODO Auto-generated method stub
		switch (v.getId()) {
		case R.id.im_back:
			finish();
			break;
		case R.id.im_cancle:
			finish();
			break;
		case R.id.im_save:
			Ruku();
			break;
		case R.id.im_che:
			if (picUrl.equals("")) {
				ToastUtil.show(this, "没有图片");
				return;
			}
			lookBigImage();
			break;
		}
	}

	private void Ruku() {
		if (HuaXinApplication.isNetWorkConnected()) {
			progress.show();
			String url = UrlUtils.ruku_url;
			Log.e("ruku_url", url);
			// TODO Auto-generated method stub
			// if (HuaXinApplication.isNetWorkConnected()) {
			if (te_daqu.getText().toString().equals("")) {
				ToastUtil.show(this, "大区不为空");
				return;
			}
			if (te_paishu.getText().toString().equals("")) {
				ToastUtil.show(this, "排数不为空");
				return;
			}
			if (te_geshu.getText().toString().equals("")) {
				ToastUtil.show(this, "个数不为空");
				return;
			}
			if (te_chengshu.getText().toString().equals("")) {
				ToastUtil.show(this, "层数不为空");
				return;
			}
			Log.e("", rowsBean.getId() + "===" + rowsBean.getStatus() + "==="
					+ te_daqu.getText().toString() + "===="
					+ te_paishu.getText().toString() + "==="
					+ te_geshu.getText().toString() + "===="
					+ te_chengshu.getText().toString());
			params = new ArrayList<NameValuePair>();
			params.add(new BasicNameValuePair("id", rowsBean.getId() + ""));
			params.add(new BasicNameValuePair("status", rowsBean.getStatus()
					+ ""));
			params.add(new BasicNameValuePair("bigarea", te_daqu.getText()
					.toString()));
			params.add(new BasicNameValuePair("rowno", te_paishu.getText()
					.toString()));
			params.add(new BasicNameValuePair("number", te_geshu.getText()
					.toString()));
			params.add(new BasicNameValuePair("plies", te_chengshu.getText()
					.toString()));
			new Thread() {

				@Override
				public void run() {
					// TODO Auto-generated method stub
					super.run();
					try {
						// String data=SimpleClient.doPost(UrlUtils.ruku_url,
						// params);
						String data = SimpleClient.doPost(UrlUtils.ruku_url,
								params);
						Log.e("ruku_save_data00", data);
						Message mes = new Message();
						mes.what = 1;
						mes.obj = data;
						handler.sendMessage(mes);
					} catch (Exception e) {
						// TODO Auto-generated catch block
						e.printStackTrace();
					}
				}

			}.start();
		} else {
			Toast.makeText(this, "网络异常", Toast.LENGTH_SHORT).show();
		}
		// new Thread(){
		//
		// @Override
		// public void run() {
		// // TODO Auto-generated method stub
		// super.run();
		// String data;
		// try {
		// data = SimpleClient.doPost(
		// UrlUtils.ruku_url, params);
		// Log.e("ruku_url_data", data);
		// Message mes = new Message();
		// mes.what = 1;
		// mes.obj = data;
		// handler.sendMessage(mes);
		// } catch (Exception e) {
		// // TODO Auto-generated catch block
		// e.printStackTrace();
		// }
		// }
		//
		// }.start();
		//
		// } else {
		// ToastUtil.show(this, "网络异常");
		// }
	}

	protected void lookBigImage() {
		// TODO Auto-generated method stub
		final ArrayList<String> urllist = new ArrayList<String>();
		urllist.add(picUrl);
		new PhotosDialog(CarRuKuDengJiActivity2.this, R.style.Dialog).show(0,
				urllist);
	}
}
